package cultu.framework.service.blaze;

import org.springframework.flex.core.MessageInterceptor;
import org.springframework.flex.core.MessageProcessingContext;

import flex.messaging.messages.Message;

import net.sf.beanlib.hibernate.HibernateBeanReplicator;
import net.sf.beanlib.hibernate3.Hibernate3BeanReplicator;
import net.sf.beanlib.hibernate3.LazyHibernateCustomBeanTransformer;

/**
 * The Class EntityCleanerInterceptor.
 */
public class EntityCleanerInterceptor implements MessageInterceptor
{
    
    /** The replicator. */
    private final HibernateBeanReplicator replicator = new Hibernate3BeanReplicator()
            .initCustomTransformerFactory(new LazyHibernateCustomBeanTransformer.Factory());

    /**
     * @see org.springframework.flex.core.MessageInterceptor#postProcess(org.springframework.flex.core.MessageProcessingContext, flex.messaging.messages.Message, flex.messaging.messages.Message)
     */
    public Message postProcess(MessageProcessingContext pContext, Message pInputMessage, Message pOutputMessage)
    {
        Object cleanEntity = replicator.copy(pOutputMessage.getBody());
        pOutputMessage.setBody(cleanEntity);
        return pOutputMessage;
    }

    /**
     * @see org.springframework.flex.core.MessageInterceptor#preProcess(org.springframework.flex.core.MessageProcessingContext, flex.messaging.messages.Message)
     */
    public Message preProcess(MessageProcessingContext pContext, Message pInputMessage)
    {
        return pInputMessage;
    }
}
